package com.weblog.spider.pipeline;

import com.weblog.spider.dao.ArticleDao;
import com.weblog.spider.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import util.IdWorker;

import java.time.LocalDateTime;

/**
 * @author MaoLin Wang
 * @date 2019/11/2421:14
 */
@Component
public class ArticlePipline implements Pipeline {

    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private IdWorker idWorker;
    @Override
    public void process(ResultItems resultItems, Task task) {
        String title=resultItems.get("title");
        String content=resultItems.get("content");
        LocalDateTime dateTime=resultItems.get("publishtime");
        Article article=new Article();
        article.setAid(idWorker.nextId()+"");
        article.setTitle(title);
        article.setContentHtml(content);
        article.setPublishtime(dateTime);
        article.setCommentnum(0);
        article.setIscomment("1");
        article.setIshot("0");
        article.setIspublic("1");
        article.setIstop("0");
        article.setState("1");
        article.setThumbup(0L);

        articleDao.save(article);
    }
}
